
Partial Class Admin_EditUser
    Inherits BasePage
    Dim username As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'retrieve the user name from the querystring
        username = Request.QueryString("username")

        lblRolesFeedbackOK.Visible = False
        lblProfileFeedbackOK.Visible = False

        If Not IsPostBack Then
            'If Request.QueryString("username") > "" Then
            UserProfile1.UserName = username
            'show the user's details
            Dim user As MembershipUser = Membership.GetUser(UserProfile1.UserName)
            lblUserName.Text = user.UserName
            lnkEmail.Text = user.Email
            lnkEmail.NavigateUrl = "mailto:" & user.Email
            lblRegistered.Text = user.CreationDate.ToString("f")
            lblLastLogin.Text = user.LastActivityDate.ToString("f")
            chkOnlineNow.Checked = user.IsOnline
            chkApproved.Checked = user.IsApproved
            chkLockedOut.Checked = user.IsLockedOut
            chkLockedOut.Enabled = user.IsLockedOut

            BindRoles()
            'Else
            'UserProfile1.UserName = ""
            'lblRolesFeedbackOK.Visible = False
            'lblProfileFeedbackOK.Visible = False
            'End If
        End If
    End Sub

    Private Sub BindRoles()
        ' fill in the checkbox list with all the available roles and then select those that the user belongs to
        chklRoles.DataSource = Roles.GetAllRoles
        chklRoles.DataBind()
        Dim role As String
        For Each role In Roles.GetRolesForUser(username)
            chklRoles.Items.FindByText(role).Selected = True
        Next
    End Sub

    Protected Sub btnUpdateRoles_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdateRoles.Click
        'first remove the user from all roles..
        Dim currRoles As Array
        currRoles = Roles.GetRolesForUser(username)
        If currRoles.Length > 0 Then
            Roles.RemoveUserFromRoles(username, currRoles)
            'and then add the user to the selected roles
        End If
        Dim newRoles As New System.Collections.Generic.List(Of String)
        Dim item As ListItem
        For Each item In chklRoles.Items
            If item.Selected Then
                newRoles.Add(item.Text)
            End If
        Next
        Roles.AddUserToRoles(username, newRoles.ToArray)
        lblRolesFeedbackOK.Visible = True

    End Sub

    Protected Sub btnCreateRole_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateRole.Click
        If Not Roles.RoleExists(txtNewRole.Text.Trim) Then
            Roles.CreateRole(txtNewRole.Text.Trim)
            BindRoles()
        End If
    End Sub

    Protected Sub chkApproved_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkApproved.CheckedChanged
        Dim user As MembershipUser = Membership.GetUser(UserProfile1.UserName)
        user.IsApproved = chkApproved.Checked
        Membership.UpdateUser(user)
    End Sub

    Protected Sub chkLockedOut_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkLockedOut.CheckedChanged
        If Not chkLockedOut.Checked Then
            Dim user As MembershipUser = Membership.GetUser(UserProfile1.UserName)
            user.UnlockUser()
            chkLockedOut.Enabled = False
        End If
    End Sub

    Protected Sub btnUpdateProfile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdateProfile.Click
        UserProfile1.SaveProfile()
        lblProfileFeedbackOK.Visible = True
    End Sub
End Class
